function regist() {
    //获取注册表单
    const oForm = document.querySelector('.form-area form');
    //获取注册表单的用户名输入框
    let oName = document.querySelector('.form-area input[name="username"]');
    //获取注册表单的密码输入框
    let oPwd = document.querySelector('.form-area input[name="password"]');

    //注册表单提交事件
    oForm.addEventListener('submit', function (e) {
        let username = oName.value.trim();
        let password = oPwd.value.trim();
        e = e || window.event;
        //阻止表单默认提交事件
        e.preventDefault()
        Success(username, password);
    });
}

function Success(username, password) {
    if (checkUsername(username) && checkPassword(password)) {

        let userArr = JSON.parse(localStorage.getItem('users')) || [];

        let userObj = {
            username,
            password
        }

        userArr.push(userObj);

        localStorage.setItem('users', JSON.stringify(userArr));


        alert('注册成功');
        window.location.href = "../login.html"
    }
}


//检查用户名
function checkUsername(username) {
    let reg = /[\u4e00-\u9fa5a-zA-Z0-9]{2,}/g;
    if (username == '') {
        alert('用户名不能为空')
        return false;
    } else {
        if (reg.test(username)) {
            return true;
        } else {
            alert('用户名不合法')
            return false;
        }
    }
}
//检查用户密码
function checkPassword(password) {
    let reg = /[a-zA-Z0-9]{8,}$/;
    if (password == '') {
        alert('密码不能为空')
        return false;
    } else {
        if (reg.test(password)) {
            return true;
        } else {
            alert('密码至少8位')
            return false;
        }
    }
}

regist()